/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Date;

import command.CommandFactory;
import config.Debug;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class PlayerMonitor implements Runnable {

	// number of minutes the player can be frozen before restarting;
	public static final int MAX_FREEZE_DURATION_IN_MINUTE = 3;
	private static long timestamp = System.currentTimeMillis();

	// private long time = System.currentTimeMillis();

	@Override
	public void run() {
		while (true) {
			long nextRecordTimeStamp = ScheduleController.getNextRecordTimestamp();
			if (nextRecordTimeStamp != PlayerMonitor.timestamp) {
				PlayerMonitor.timestamp = nextRecordTimeStamp;
			} else {
				if (System.currentTimeMillis() > PlayerMonitor.timestamp + 1000 * 60 * MAX_FREEZE_DURATION_IN_MINUTE) {
					Debug.log("Player froze.", Debug.ERROR_LEVEL);
					System.out.println(new Date(System.currentTimeMillis())+ " - " + new Date(PlayerMonitor.timestamp));
					CommandFactory.getInstance().restartPlayer();
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		
	}
}
